package com.qf.gh.dao.impl;

import com.qf.gh.dao.AddressDao;
import com.qf.gh.domain.Address;
import com.qf.gh.utils.DateSourceUtils;
import com.qf.gh.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> findByUid(int id) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="select  id , detail, name ,phone ,uid  ,level from tb_address where uid=? ";
        try {
            List<Address> addressList = qr.query(sql, new BeanListHandler<>(Address.class), id);
             return addressList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addAddress(Address address) {
        QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        String sql="insert into tb_address values(null,?,?,?,?,?) ";
        try {
            qr.update(sql,address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel());

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("新增地址失败");
        }

    }

    @Override
    public void update(Address address) {
        QueryRunner qr = new QueryRunner(DateSourceUtils.getDataSource());
        String sql = "UPDATE `tb_address` SET `detail`=?, `name`=?, `phone`=?, `uid`=?, `level`=? WHERE (`id`=?)";
        try {
            qr.update(sql , address.getDetail(), address.getName(), address.getPhone(),address.getUid(),address.getLevel(), address.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("更改地址失败");
        }
    }

    @Override
    public void deleteById(int parseInt) {
     String sql="delete from tb_address where id=?";
     QueryRunner qr=new QueryRunner(DateSourceUtils.getDataSource());
        try {
            qr.update(sql,parseInt);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除地址失败");
        }
    }

    @Override
    public void clearDefaultAddress(int id) {
        Connection conn = TxUtils.getConn();
        String sql="UPDATE tb_address set level= 0 WHERE uid= ?";
        QueryRunner qr=new QueryRunner();
        try {
            qr.update(conn,sql,id);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("清楚默认地址失败");
        }
    }

    @Override
    public void setDefaultAddress(int parseInt) {
        Connection conn = TxUtils.getConn();
        String sql="UPDATE tb_address set level= 1 WHERE id= ?";
        QueryRunner qr=new QueryRunner();
        try {
            qr.update(conn,sql,parseInt);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("设置默认地址失败");
        }
    }
}
